Contents

1 Preamble

1.1 Dependencies

library(tidyr)
library(ggplot2)
library(patchwork)
library(SpatialData)
library(SingleCellExperiment)

1.2 Load data

path <- file.path("..", "inst", "extdata", "mibitof")
(spd <- readSpatialData(path))
## class: SpatialData
## table: 36 3309 
## images(3): point16_image point23_image point8_image
## labels(3): point16_labels point23_labels point8_labels
## shapes(0):
## points(0):
## coords(0):

2 Visualization

2.1 Images

ps <- lapply(images(spd), plotElement)
wrap_plots(ps, nrow = 1)

2.2 Labels

ps <- lapply(labels(spd), plotElement)
wrap_plots(ps, nrow = 1)

2.3 Layered

ps <- mapply(
    \(i, l) plotSD(spd, image = i, label = l),
    SIMPLIFY = FALSE, i = imageNames(spd), l = labelNames(spd))
wrap_plots(ps, nrow = 1)

3 Aggregation

3.1 Image by label

# TODO: one function call should aggregate
# across all matching coordinate systems
ms <- mapply(
    \(i, l) {
        sce <- aggregateImage(spd, image = i, label = l)
        sce$id <- i; return(sce)
    },
    SIMPLIFY = FALSE, i = imageNames(spd), l = labelNames(spd))
(sce <- do.call(cbind, ms))
## class: SingleCellExperiment 
## dim: 3 3771 
## metadata(0):
## assays(1): mean
## rownames: NULL
## rowData names(0):
## colnames(3771): 1 2 ... 1180 1181
## colData names(3): x y id
## reducedDimNames(0):
## mainExpName: NULL
## altExpNames(0):

3.2 Visualization

chs <- rownames(sce) <- c("R", "G", "B")
df <- data.frame(colData(sce), t(assay(sce)), check.names = FALSE)
fd <- pivot_longer(df, all_of(chs))
fd$name <- factor(fd$name, chs)
ggplot(fd, aes(x, y, col = value)) +
    geom_point(size = 1) + facet_grid(id ~ name) +
    scale_color_viridis_c(option = "C", trans = "sqrt") +
    scale_y_reverse() + coord_equal() + 
    theme_linedraw() + theme(
        axis.title = element_blank(),
        panel.grid = element_blank(),
        panel.background = element_rect(fill = "black"),
        strip.background = element_rect(fill = "black", color = "white"))

4 Appendix

4.1 Session info

sessionInfo()
## R version 4.3.0 (2023-04-21)
## Platform: aarch64-apple-darwin20 (64-bit)
## Running under: macOS Ventura 13.2.1
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
## LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## time zone: Europe/Zurich
## tzcode source: internal
## 
## attached base packages:
## [1] stats    graphics utils    stats4   methods  base    
## 
## other attached packages:
##  [1] SingleCellExperiment_1.22.0 SummarizedExperiment_1.30.1
##  [3] Biobase_2.60.0              GenomicRanges_1.52.0       
##  [5] GenomeInfoDb_1.36.0         IRanges_2.34.0             
##  [7] S4Vectors_0.38.1            BiocGenerics_0.46.0        
##  [9] MatrixGenerics_1.12.0       matrixStats_0.63.0         
## [11] SpatialData_0.99.0          patchwork_1.1.2            
## [13] ggplot2_3.4.2               tidyr_1.3.0                
## [15] BiocStyle_2.28.0           
## 
## loaded via a namespace (and not attached):
##  [1] tidyselect_1.2.0        viridisLite_0.4.2       EBImage_4.42.0         
##  [4] farver_2.1.1            dplyr_1.1.2             filelock_1.0.2         
##  [7] arrow_11.0.0.3          R.utils_2.12.2          bitops_1.0-7           
## [10] fastmap_1.1.1           RCurl_1.98-1.12         digest_0.6.31          
## [13] lifecycle_1.0.3         paws.storage_0.2.0      magrittr_2.0.3         
## [16] compiler_4.3.0          rlang_1.1.1             sass_0.4.6             
## [19] tools_4.3.0             utf8_1.2.3              yaml_2.3.7             
## [22] knitr_1.42              labeling_0.4.2          S4Arrays_1.0.1         
## [25] htmlwidgets_1.6.2       bit_4.0.5               curl_5.0.0             
## [28] here_1.0.1              reticulate_1.28         DelayedArray_0.26.1    
## [31] abind_1.4-5             zellkonverter_1.10.0    withr_2.5.0            
## [34] purrr_1.0.1             R.oo_1.25.0             grid_4.3.0             
## [37] fansi_1.0.4             grDevices_4.3.0         colorspace_2.1-0       
## [40] scales_1.2.1            cli_3.6.1               rmarkdown_2.21         
## [43] crayon_1.5.2            generics_0.1.3          rstudioapi_0.14        
## [46] httr_1.4.5              cachem_1.0.8            stringr_1.5.0          
## [49] zlibbioc_1.46.0         datasets_4.3.0          parallel_4.3.0         
## [52] assertthat_0.2.1        BiocManager_1.30.20     XVector_0.40.0         
## [55] tiff_0.1-11             basilisk_1.12.0         vctrs_0.6.2            
## [58] Matrix_1.5-4            dir.expiry_1.8.0        jsonlite_1.8.4         
## [61] bookdown_0.33           fftwtools_0.9-11        bit64_4.0.5            
## [64] magick_2.7.4            jpeg_0.1-10             locfit_1.5-9.7         
## [67] jquerylib_0.1.4         glue_1.6.2              codetools_0.2-19       
## [70] stringi_1.7.12          gtable_0.3.3            Rarr_1.0.0             
## [73] munsell_0.5.0           tibble_3.2.1            pillar_1.9.0           
## [76] basilisk.utils_1.12.0   htmltools_0.5.5         GenomeInfoDbData_1.2.10
## [79] R6_2.5.1                rprojroot_2.0.3         evaluate_0.21          
## [82] lattice_0.21-8          highr_0.10              R.methodsS3_1.8.2      
## [85] png_0.1-8               paws.common_0.5.6       bslib_0.4.2            
## [88] Rcpp_1.0.10             xfun_0.39               pkgconfig_2.0.3